package com.yj.suncity.user;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UpdatePwdActivity1 extends Activity {
	private static final int TIMER = 101;
	private static final String TAG = "UpdatePwdActivity1";
	private Context context;
	private Button mBtn_verification_code;

	private String userPhone;
	private String titleName;
	private int flag;// 1，修改密码；2，忘记密码
	private int typeId;

	private String mStr_verification_code = "";
	private EditText mEdt_phone, mEdt_verification_code;
	private Timer mTimer;
	private int mTime = 60;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case TIMER:
				if (mTime <= 0) {
					mTimer.cancel();
					mTimer = null;
					// task.cancel();
					// task=null;
					mTime = 60;
					btnIsValid();
				} else {
					btnIsUnvalid(mTime + "秒后重新获取");
				}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_updata_pwd1);
		context=this;
		titleName = getIntent().getStringExtra("titlename");
		userPhone=getIntent().getStringExtra("userphone");
		flag = getIntent().getIntExtra("flag", -1);
		findView();
	}

	private void findView() {
		((TextView) findViewById(R.id.txt_title_name)).setText(titleName);

		mBtn_verification_code = (Button) findViewById(R.id.btn_verifica_code);
		mEdt_phone = (EditText) findViewById(R.id.edt_user_phone);
		mEdt_verification_code = (EditText) findViewById(R.id.edt_verification_code);
		
		if (flag==1) {
			typeId=3;
			mEdt_phone.setEnabled(false);
			mEdt_phone.setClickable(false);
			mEdt_phone.setText(userPhone);
		} else {
			typeId=4;
		}
	}

	// 获取验证码按钮有效
	private void btnIsValid() {
		mBtn_verification_code.setClickable(true);
		mBtn_verification_code.setEnabled(true);
		mBtn_verification_code.setText("获取验证码");
		mBtn_verification_code.setTextColor(getResources().getColor(
				R.color.orange));
	}

	// 获取验证码按钮无效
	private void btnIsUnvalid(String btnText) {
		mBtn_verification_code.setClickable(false);
		mBtn_verification_code.setEnabled(false);
		mBtn_verification_code.setText(btnText);
		mBtn_verification_code.setTextColor(getResources().getColor(
				R.color.txt_gray_light));
	}

	private void sendVerification(String phone) {
		String path = URLCon.短信接口 + "?action=sendsecuritycode&typeid=" + typeId
				+ "&mobile=" + phone;
		Log.d(TAG, path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在发送验证码......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							mStr_verification_code = jsonObject
									.optString("code");
							Log.d(TAG, "code==" + mStr_verification_code);
							ToastUtil.ShowToast(context, "发送成功~~~");
							btnIsUnvalid("60秒后重新获取");
							mTimer = new Timer();
							mTimer.schedule(new TimerTask() {
								@Override
								public void run() {
									mTime--;
									Message message = new Message();
									message.what = TIMER;
									handler.sendMessage(message);
								}
							}, 1000, 1000);
						} else {
							btnIsValid();
							ToastUtil.ShowToast(context, "发送失败！");
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_verifica_code:
			String phone = mEdt_phone.getText().toString().trim();
			if (phone.equals("")) {
				ToastUtil.ShowToast(context, "请输入手机号码！");
				return;
			}
			if (!Utils.checkMobilePhone(phone)) {
				ToastUtil.ShowToast(context, "手机号码不正确！");
				return;
			}
			sendVerification(phone);
			break;
		case R.id.btn_regist:
			String phone1 = mEdt_phone.getText().toString().trim();
			String ver_code = mEdt_verification_code.getText().toString()
					.trim();

			if (phone1.equals("")) {
				ToastUtil.ShowToast(context, "请输入手机号码！");
				return;
			}
			if (!Utils.checkMobilePhone(phone1)) {
				ToastUtil.ShowToast(context, "手机号码不正确！");
				return;
			}

			// 验证码
			if (ver_code.equals("")) {
				ToastUtil.ShowToast(context, "请输入验证码！");
				return;
			}
			if (!ver_code.equals(mStr_verification_code)) {
				ToastUtil.ShowToast(context, "验证码不正确！");
				return;
			}
			Intent intent = new Intent(this, UpdatePwdActivity2.class);
			intent.putExtra("phone", phone1);
			startActivityForResult(intent, 1);

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == 1) {
			this.finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
